$(function () {
    bindAccountZfbFn()    //绑定支付宝
});

//绑定成功失败 反馈
var bindAccountFn = function (title,text,success,callback) {
    var context = '<div class="pr pt5 pb5">';
    if(success){
        context += '<aside class="exam-stop-icon applyWithdraw-icon"><span>&nbsp;</span></aside>';
    }else{
        context += '<aside class="exam-stop-icon checkInfo-icon"><span>&nbsp;</span></aside>';
    }
    context += '<section class="text-center">';
    context += '<h3 class="" style="margin-bottom: 0;font-weight: 200;">'+title+'</h3>';
    context += '<section class="pt5">';
    context += '<span class="fs6 c-888">'+text+'</span>';
    context += '</section>';
    context +='</section>';
    context +='</div>';
    $.alert(context,function () {
        if($.isFunction(callback)){
            callback();
        }
    });
    $('.modal-in').find('.modal-button-bold').text('知道了').attr('style', '');
}


var bindingAppWithWeChat = function(){
    var  noMobile  = getCookie("noMobile");
    if(noMobile!=null){
        if(noMobile=="1"){
            android.bindingWithWechat();
        }

    }else{
        bindAccountWxFn();
    }
};

//绑定微信
var bindAccountWxFn = function () {
    var _ele = $('#bindAccountWx-ele').val();
    if(!isWeiXin()){
        bindAccountFn('禁止操作',"微信号绑定必须在微信环境中操作！",false);
        return;
    }
    $.confirm(_ele, '',
        function (em) {
            var wxRealName = em.find("#wxRealName").val();
            if(wxRealName==null || $.trim(wxRealName)==''){
                bindAccountFn('操作失败','请输入微信账号的真实姓名！',false);
            }else{
                //绑定微信下一步
                bindAccountWxNextFn(wxRealName)
            }
        }
    );
    $('.modal-in').find('.modal-button').css('color', '#bbb');
    $('.modal-in').find('.modal-button-bold').text('下一步').attr('style', '');
};
//绑定微信下一步
var bindAccountWxNextFn = function (wxRealName) {
    $.ajax({
        url: baseloaction + '/mobile/uc/cash/bindingWeiXin',
        type:'post',
        dataType:'json',
        data:{
            'realName':wxRealName
        },
        success:function (result) {
            if(result.success==true){
                window.location.href = result.entity;
            }else{
                bindAccountFn('操作失败',result.message,false);
            }
        },
        error:function (error) {
            bindAccountFn('操作失败','系统错误，绑定微信账号失败！',false);
        }
    });
};

//绑定支付宝
var bindAccountZfbFn = function () {
    var _ele = $('#bindAccountZfb-ele').val();
    $(document).on('click', '.bindAccountZfbBtn', function () {
        $.confirm(_ele, '',
            function (em) {
                var zfbRealName = em.find('#zfbRealName').val();
                var zfbAccount = em.find('#zfbAccount').val();
                $.ajax({
                    url:baseloaction +'/mobile/uc/cash/saveZfbCashAccount',
                    type:'post',
                    dataType:'json',
                    data:{
                        'realName':zfbRealName,
                        'account':zfbAccount
                    },
                    success:function (result) {
                        if(result.success==true){
                            bindAccountFn('绑定成功',"绑定支付宝账号成功",true,function () {
                                window.location.href = baseloaction + '/mobile/uc/cash/findUserCashAccount';
                            });
                        }else{
                            bindAccountFn('操作失败',result.message,false);
                        }
                    },
                    error:function (error) {
                        bindAccountFn('操作失败','系统错误，绑定支付宝账号失败',false);
                    }
                });
            }
        );
        $('.modal-in').find('.modal-button').css('color', '#bbb');
        $('.modal-in').find('.modal-button-bold').text('完成').attr('style', '');
    })
};


//绑定微信下一步
var appBindAccountWxNextFn = function (openId) {
    var _ele = $('#bindAccountWx-ele').val();
    $.confirm(_ele, '',
        function (em) {
            var wxRealName = em.find("#wxRealName").val();
            if(wxRealName==null || $.trim(wxRealName)==''){
                bindAccountFn('操作失败','请输入微信账号的真实姓名！',false);
            }else{
                putWeuXin(wxRealName,openId)
            }
        }
    );

};


function putWeuXin(wxRealName,openId){
    console.log(openId);
    $.ajax({
        url: baseloaction + '/mobile/uc/cash/saveAppWeiXinAccount',
        type:'post',
        dataType:'json',
        data:{
            'realName':wxRealName,"openId":openId
        },
        success:function (result) {
            if(result.success==true){
                window.location.reload();
            }else{
                bindAccountFn('操作失败',result.message,false);
            }
        },
        error:function (error) {
            bindAccountFn('操作失败','系统错误，绑定微信账号失败！',false);
        }
    });
}